import { reqTradeInfo, reqUserAddressList } from "@/api";

const state = {
  tradeInfo: {},
  addressList: [] // 地址列表
}
const mutations = {
  SAVE_ADDRESSLIST(state, addressList) {
    state.addressList = addressList;
  },
  SAVE_TRADEINFO(state, tradeInfo) {
    state.tradeInfo = tradeInfo;
  }
}
const actions = {
  // 获取地址列表
  async getAddressList({ commit }) {
    try {
      let result = await reqUserAddressList();
      if (result && result.code == 200) {
        commit('SAVE_ADDRESSLIST', result.data)
      } else {
        return Promise.reject(result.message)
      }
    } catch (error) {
      return Promise.reject(error)
    }
  },
  // 获取交易信息
  async getTradeInfo({ commit }) {
    try {
      let result = await reqTradeInfo();
      if (result && result.code == 200) {
        commit('SAVE_TRADEINFO', result.data)
      } else {
        return Promise.reject(result.message)
      }
    } catch (error) {
      return Promise.reject(error)
    }
  }
}
const getters = {
  goodsList(state) {
    return state.tradeInfo.detailArrayList || []
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
}

